
package com.timber.util;

import java.text.ParseException;

/**
  *  This class handles the formatting for Strings.
  *  No formatting or parsing is necessary as the object is
  *  already in the form of a String.
  *
  *  Additional validation is provided:
  *  <li> Maximum Length
  *  <li> forcing characters to upper case
  *  <li> restricting characters to a specific set of characters
  *
  *  @author
  *  @date
  */


public class StringFormatter extends Formatter
{
  protected int maxLength;
  protected boolean forceUpperCase;
  protected char[] allowableChars;
  protected String prefix;           // Add a prefix to the value

  /**
   * Constructor
   * @param maxLength maximum length of string
   * @param upper whether or not to force entered text to uppercase
   * @param character array of allowable characters
   */
  public StringFormatter( int maxLength, boolean upper, String prefix, char[] allowableChars )
  {
    this.maxLength = maxLength;
    this.forceUpperCase = upper;
    this.prefix = prefix;
    this.allowableChars = allowableChars;
  }

  /**
   * Constructor
   * Creates a StringFormatter with no max length and forces text to upper case
   */
  public StringFormatter ()
  {
    this.maxLength = 0;
    this.prefix = null;
    this.forceUpperCase = true;
    this.allowableChars = null;
  }

  /**
   * Formats an object to produce a string.
   */
  public String format( Object o )
  {
    String out = (o == null) ? "" : o.toString();

    return ( prefix != null ) ? (prefix + out) : out;
  }

  public Class getDataType()
  {
    return String.class;
  }

  public Object parseObject( String s ) throws ParseException
  {
    return parse( s );
  }

  public String parse( String s ) throws ParseException
  {
    boolean validChar = true;

    if( maxLength > 0 && s.length() > maxLength )
      throw new ParseException( "String: " +
				s +
				" exceeds maximum format length: " +
				this.maxLength,
				maxLength );

    if( allowableChars != null )
    {
      int length = s.length();

      for( int i = 0; i < length; i++ )
      {
	validChar = false;
	for( int j = 0; j < allowableChars.length; j++ )
	{
	  if( s.charAt( i ) == allowableChars[ j ] )
	  {
	    validChar = true;
	    break;
	  }
	}

	if( validChar == false )
	  throw new ParseException( "String: " +
				    s +
				    " contains unallowable character: " +
				    s.charAt( i ),
				    i );
      }
    }
    return s;
  }

  /**
   *  Validate newChar to see if it is an allowable character
   *
   */
  public boolean accepts( String oldVal, int oldPos, char newChar, String newVal, int newPos )
  {
    // First return false if length exceeds maximum
    if( maxLength > 0 && newVal.length() > maxLength )
      return false;

    // Next check if characters are allowable
    if( allowableChars != null )
    {
      for( int i = 0; i < allowableChars.length; i++ )
      {
	if( newChar == allowableChars[ i ] )
	  return true;
      }
      return false;
    }
    return true;
  }

  public static String formatStr(String str, int size)
  {
    int len = 0;

    if (str != null)
       len = str.length();

    if(len>size)
      return str.substring(0,size);

    for(int i=0;i<size-len;i++) str=str+" ";

    return str;
  }

}
